using System;
using System.Collections.Generic;
using System.Text;
using TVDesigner.Entities.Elements;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace TVDesigner.Entities.DataProvider
{
    public class DataProvider
    {
        public static void SaveProject(ProjectXlet project, string path)
        {
            string file = string.Concat(path, "\\", project.Name, ".tvd");
            TextWriter writer = new StreamWriter(file);
            XmlSerializer serializer = new XmlSerializer(typeof(ProjectXlet));
            serializer.Serialize(writer, project);
            writer.Close();
        }

        public static ProjectXlet OpenProject(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ProjectXlet));
            FileStream fs = new FileStream(fileName, FileMode.Open);
            XmlReader reader = new XmlTextReader(fs);
            ProjectXlet project = (ProjectXlet)serializer.Deserialize(reader);

            foreach (Screen screen in project.Screens)
            {
                screen.SetProject(project);

                foreach (GenericComponent gc in screen.Components)
                {
                    gc.ParentScreen = screen;
                }
            }
            fs.Close();

            return project;
        }
    }
}
